package com.iss.yunpan.controller;

import com.iss.yunpan.entity.Users;
import com.iss.yunpan.service.PasswordChangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PasswordChangeController {

    @Autowired
    private PasswordChangeService service;
    //前往密码修改页面
    @RequestMapping("password_change")
    public String toPassWordChange() {return "password_change";}
    @RequestMapping("changeUser")
    public String PasswordChange(Model model, String user_id, String password_old, String password_new_1) {

        Users user = service.getUsersByUsernameAndPassword(user_id,password_old);
        if(user==null){
            model.addAttribute("msg","用户名不存在或者密码不正确");
            return "password_change";
        }else {
            Integer result = service.changePassword(user_id, password_new_1);
            if(result != 1){
                model.addAttribute("msg","密码修改失败，原因未知");
                return "password_change";
            }else{
                System.out.println(user.getRealName());
                model.addAttribute("users",user);
                return "main";
            }
        }
    }
    @RequestMapping("main")
    public  String toMain(){return "main";}
}
